package a.b.reggie.controller;

import a.b.reggie.common.R;
import a.b.reggie.dto.DishDto;
import a.b.reggie.pojo.Category;
import a.b.reggie.pojo.Dish;
import a.b.reggie.service.ICategoryService;
import a.b.reggie.service.IDishFlavorService;
import a.b.reggie.service.IDishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private IDishService dishService;

    @Autowired
    private IDishFlavorService dishFlavorService;

    @Autowired
    private ICategoryService categoryService;


    /**
     * 获取菜品分页信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        //构造分页构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        //构造条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getPrice);

        //执行分页查询
        dishService.page(pageInfo, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();

        List<DishDto> list = new ArrayList<>();

        for (Dish dish:records){
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(dish,dishDto);

            Long categoryId = dish.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);

            if (category !=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
                list.add(dishDto);
            }

        }

        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);
    }

    /**
     * 根据id查询菜品信息以及菜品口味
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable("id") Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }


    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }


    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto){
        dishService.savaWithFlavor(dishDto);
        return R.success("添加成功");
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long[] ids){
        //数组转化为集合
        List<Long> idList = Arrays.asList(ids);
        log.info("idList"+idList);
        dishService.deleteWithFlavor(idList);
        return R.success("删除成功");
    }


    /**
     * 起售禁售
     * @param num
     * @param ids
     * @return
     */
    @PostMapping("/status/{num}")
    public R<String> status(@PathVariable("num") int num,Long[] ids){
        //数组转化为集合
        List<Long> idList = Arrays.asList(ids);
        //根据id集合查询菜品集合
        List<Dish> dishes = dishService.listByIds(idList);
        //修改菜品的status
        for (Dish dish:dishes){
                dish.setStatus(num);
        }
        //将修改后的菜品集合保存
        dishService.updateBatchById(dishes);
        return R.success("更改成功");
    }

    /**
     * 客户端获取
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> getCategoryById(Long categoryId){
        List<DishDto> list = dishService.userList(categoryId);
        return R.success(list);
    }








}
